import Vue from 'vue'
import Vuex from 'vuex'
import axios from '../utils/axios1'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    table:[],
  },
  mutations: {
    setList(state,payload){
       state.table=payload
    },
    // 新增
    addNum(state,payload){
      state.table.unshift(payload)
    },
    // 编辑
    edit(state,payload){
      let lists=[...state.table];
      let newList=[];
      lists.map(item=>{
        if(item.id==payload.id){
          newList.push(payload)
        }else{
          newList.push(item)
        }
      })
      state.table=newList
    },
    // 删除、
    del(state,payload){
      let arr=[];
      state.table.forEach(item=>{
        if(item.id!=payload.id){
          arr.push(item)
        }
        state.table=arr
      })
    },
    // 搜索
    search(state,payload){
      let arrs=[];
      state.table.forEach(item=>{
        if(item.id.includes(payload)||item.name.includes(payload)||
        item.tle.includes(payload)){
          arrs.push(item)
        }
      })
      state.table=arrs
    }
  },
  actions: {
    getList(context){
      axios.get('/api/table').then(res=>{
        //  console.log(context);
        context.commit('setList',res.data.data)
      })
    }
  },
  modules: {
  }
})
